<?php
/**
 * User: 1971229701@qq.com
 * Date: 2020/11/2/002
 * Time: 10:39
 */

namespace app\admin\logic;


use app\common\logic\BaseLogic;
use app\common\model\Consult;
use app\common\model\ConsultItem;
use think\Db;
use think\Exception;

class ConsultLogic extends BaseLogic
{

    public function listing($page_size = 10)
    {
        $where = [];

        $r = Consult::where($where)->order('create_time','desc')->paginate($page_size)->toArray();
        foreach ($r['data'] as &$val) {
            $val['source'] = ConsultItem::where('consult_id', $val['id'])->value('source');
        }
        return $r;
    }

    public function create($data)
    {
        $source = $data['source'];
        if (!array_key_exists($source, ConsultItem::$source)) {
            $this->error = '来源错误!';
            return false;
        }

        $item_data['source'] = ConsultItem::$source[$source];
        Db::startTrans();
        try {
            $consult_m = new Consult();
            $data['create_time'] = isset($data['create_time'])?strtotime($data['create_time']):time();
            $consult_m->allowField(true)->save($data);

            $item_data['consult_id'] = $consult_m->id;
            $item_data['content'] = $data['content'];
            $consult_item_m = new ConsultItem();
            $consult_item_m->save($item_data);
            Db::commit();
        } catch (Exception $e) {
            Db::rollback();
            $this->error = $e->getMessage();
            return false;
        }

        return true;
    }

    public function update($data, $methods = 'post')
    {

        if ($methods === 'get') {
            $consult = Consult::get($data)->toArray();
            $consult_item = ConsultItem::get(['consult_id' => $data])->toArray();
            $r = array_merge($consult, $consult_item);
            return $r;
        } else {
            $source = $data['source'];
            if (!array_key_exists($source, ConsultItem::$source)) {
                $this->error = '来源错误!';
                return false;
            }

            if (empty($data['image']))
                unset($data['image']);

            $item_data['source'] = ConsultItem::$source[$source];
            Db::startTrans();
            try {
                $consult_m = new Consult();
                $data['create_time'] = isset($data['create_time'])?strtotime($data['create_time']):time();
                $consult_m->allowField(true)->update($data);


                $item_data['content'] = $data['content'];
                $consult_item_m = new ConsultItem();
                $consult_item_m->update($item_data, ['consult_id' => $data['id']]);
                Db::commit();
            } catch (Exception $e) {
                Db::rollback();
                $this->error = $e->getMessage();
                return false;
            }
        }
        return true;
    }

    public function delete($ids)
    {
        Db::startTrans();
        try {
            $str = trim($ids, ',');
            $_arr = explode(',', $str);
            $where[] = ['id', 'in', $_arr];

            $r = Consult::where($where)->delete();

            $item_where[] = ['consult_id', 'in', $_arr];
            ConsultItem::where($item_where)->delete();
            Db::commit();
        } catch (Exception $e) {
            Db::rollback();
            $this->error = $e->getMessage();
            return false;
        }

        return true;
    }
}